package cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.oldman;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.system.enums.common.SexEnum;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.oldman.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.oldman.OldmanDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.oldman.OldmanConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.oldman.OldmanService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 老人管理")
@RestController
@RequestMapping("/fengdong.smartelderly/oldman")
@Validated
public class OldmanController {

    @Resource
    private OldmanService oldmanService;

    @PostMapping("/create")
    @Operation(summary = "创建老人管理")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman:create')")
    public CommonResult<Long> createOldman(@Valid @RequestBody OldmanCreateReqVO createReqVO) throws IOException {
        return success(oldmanService.createOldman(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新老人管理")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman:update')")
    public CommonResult<Boolean> updateOldman(@Valid @RequestBody OldmanUpdateReqVO updateReqVO) throws IOException {
        oldmanService.updateOldman(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除老人管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman:delete')")
    public CommonResult<Boolean> deleteOldman(@RequestParam("id") Long id) {
        oldmanService.deleteOldman(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得老人管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman:query')")
    public CommonResult<OldmanRespVO> getOldman(@RequestParam("id") Long id) {
        OldmanDO oldman = oldmanService.getOldman(id);
        return success(OldmanConvert.INSTANCE.convert(oldman));
    }

    @GetMapping("/list")
    @Operation(summary = "获得老人管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman:query')")
    public CommonResult<List<OldmanRespVO>> getOldmanList(@RequestParam("ids") Collection<Long> ids) {
        List<OldmanDO> list = oldmanService.getOldmanList(ids);
        return success(OldmanConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得老人管理分页")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman:query')")
    public CommonResult<PageResult<OldmanRespVO>> getOldmanPage(@Valid OldmanPageReqVO pageVO) {
        PageResult<OldmanDO> pageResult = oldmanService.getOldmanPage(pageVO);
        return success(OldmanConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出老人管理 Excel")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman:export')")
    @OperateLog(type = EXPORT)
    public void exportOldmanExcel(@Valid OldmanExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<OldmanDO> list = oldmanService.getOldmanList(exportReqVO);
        // 导出 Excel
        List<OldmanExcelVO> datas = OldmanConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "老人管理.xls", "数据", OldmanExcelVO.class, datas);
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入长者模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<OldmanImportExcelVO> list = Arrays.asList(
                OldmanImportExcelVO.builder().name("老人姓名").identityCard("老人身份证").phoneNumber("老人手机号").age(90)
                        .guardianName("监护人姓名").guardianCard("监护人身份证").guardianNumber("监护人手机号").mark("备注")
                        .roomId(12).qrUrl("").point(0).type(CommonStatusEnum.ENABLE.getStatus()).sex(SexEnum.MALE.getSex()).build()
        );

        // 输出
        ExcelUtils.write(response, "用户导入模板.xls", "用户列表", OldmanImportExcelVO.class, list);
    }

    @PostMapping("/import-excel")
    @Operation(summary = "长者导入")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman:import')")
    public CommonResult<OldManIImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<OldmanImportExcelVO> list = ExcelUtils.read(file, OldmanImportExcelVO.class);
        return success(oldmanService.importOldmanList(list, updateSupport));
    }

}
